﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using WoSoft.PACS.Data.BaseData;
using WoSoft.PACS.Data.Models;
using WoSoft.PACS.Data.Patient;

namespace WoSoft.PACS.Win.Forms.SubForms
{
    /// <summary>
    /// 病人信息
    /// </summary>
    public partial class FrmPatientInfo : FrmBase
    {
        /// <summary>
        /// ID
        /// </summary>
        public string ID { get; set; }

        PatientData patientdata = new PatientData();

        public FrmPatientInfo()
        {
            InitializeComponent();
        }

        //加载
        private void FrmPatientInfo_Load(object sender, EventArgs e)
        {
            //加载民族数据源
            LoadNationList(this.cbNation);

            //加载性别列表
            LoadSexList(this.cbSex);

            //加载学历
            LoadEducationList(this.cbEducation);

            //加载婚姻
            LoadMarriageList(this.cbMarriage);

            //加载支付方式
            LoadPayModeList(this.cbPaymode);

            //加载户籍类型
            LoadAccountList(this.cbAccountType);

            //加载职业
            LoadWorker(this.cbWorker);

            this.errorProvider1.Clear();
            this.dtBirthday.Value = DateTime.Now;

            //加载ID数据
            if (!string.IsNullOrEmpty(this.ID))
            {
                PatientModel patientModel = patientdata.GetModelByID(this.ID);
                if (patientModel != null)
                {
                    this.tbName.Text = patientModel.姓名;
                    this.cbSex.SelectedValue = patientModel.性别;
                    this.tbCode.Text = patientModel.简码;
                    this.dtBirthday.Value = Convert.ToDateTime(patientModel.出生日期);
                    this.tbIdentityCode.Text = patientModel.身份证号;
                    this.tbWorkaddress.Text = patientModel.工作单位;
                    this.tbPhone.Text = patientModel.本人电话;
                    this.tbEmail.Text = patientModel.电子邮件;
                    this.tbContName.Text = patientModel.联系人电话;
                    this.tbContPhone.Text = patientModel.联系人姓名;
                    this.cbAccountType.SelectedValue = patientModel.常住类型;
                    this.tbFJAddress.Text = patientModel.户籍地址;
                    this.tbHomeAddress.Text = patientModel.现住址;
                    this.tbPostCode.Text = patientModel.邮政编码;
                    this.cbNation.SelectedValue = patientModel.民族;
                    this.cbEducation.SelectedValue = patientModel.文化程度;
                    this.cbWorker.SelectedValue = patientModel.职业;
                    this.cbMarriage.SelectedValue = patientModel.婚姻状况;
                    this.cbPaymode.SelectedValue = patientModel.医疗付款方式;
                }
            }
        }


        //关闭
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //确定保存
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.tbName.Text.Trim().Length <= 0)
            {
                errorProvider1.SetError(this.tbName, "姓名为必填项目");
                return;
            }

            var patientModel = new PatientModel();
            if (!string.IsNullOrEmpty(this.ID))
                patientModel.ID = this.ID;
            else
                patientModel.ID = "";
            patientModel.姓名 = this.tbName.Text.Trim();
            patientModel.性别 = Convert.ToInt32(this.cbSex.SelectedValue);
            patientModel.简码 = this.tbCode.Text.Trim();
            patientModel.出生日期 = this.dtBirthday.Value;
            patientModel.身份证号 = this.tbIdentityCode.Text.Trim();
            patientModel.工作单位 = this.tbWorkaddress.Text.Trim();
            patientModel.本人电话 = this.tbPhone.Text.Trim();
            patientModel.电子邮件 = this.tbEmail.Text.Trim();
            patientModel.联系人电话 = this.tbContName.Text.Trim();
            patientModel.联系人姓名 = this.tbContPhone.Text.Trim();
            patientModel.常住类型 = Convert.ToInt32(this.cbAccountType.SelectedValue);
            patientModel.户籍地址 = this.tbFJAddress.Text.Trim();
            patientModel.现住址 = this.tbHomeAddress.Text.Trim();
            patientModel.邮政编码 = this.tbPostCode.Text.Trim();
            patientModel.民族 = this.cbNation.SelectedValue.ToString();
            patientModel.文化程度 = Convert.ToInt32(this.cbEducation.SelectedValue);
            patientModel.职业 = this.cbWorker.SelectedValue.ToString();
            patientModel.婚姻状况 = Convert.ToInt32(this.cbMarriage.SelectedValue);
            patientModel.医疗付款方式 = Convert.ToInt32(this.cbPaymode.SelectedValue);
            patientModel.建档人 = Program.CurrentLoginUser.Name;

            var patientData = new PatientData();
            var cnt = patientData.Insert(patientModel);
            if (cnt > 0 && string.IsNullOrEmpty(this.ID))
            {
                MessageBoxEx.Show("新建病人档案成功", "创建档案", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            if (cnt > 0 && !string.IsNullOrEmpty(this.ID))
            {
                MessageBoxEx.Show("修改病人档案成功", "修改档案", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }

        private void tbAge_Leave(object sender, EventArgs e)
        {
            var v = this.tbAge.Value;
            this.dtBirthday.Value = DateTime.Now.AddYears(-v);
        }
    }
}
